/***
 * @Author: 玄绮 shijiahansi@qq.com
 * @Date: 2024-05-25 18:29:21
 * @LastEditors: 玄绮 shijiahansi@qq.com
 * @LastEditTime: 2024-05-25 18:30:27
 * @FilePath: \server\src\tools\tools.cpp
 * @Description:
 * @
 * @Copyright (c) 2024 by 玄绮, All Rights Reserved.
 */

#include <boost/algorithm/algorithm.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/xpressive/regex_algorithms.hpp>
#include <boost/xpressive/xpressive_dynamic.hpp>
#include <fstream>
#include <iostream>
#include <random>
#include <smtp>
#include <string>
#include <tools.h>

using namespace boost::xpressive;

static sregex_compiler rc;

namespace tools {
    int generateVerificationCode() {
        // 使用 C++11 的随机数引擎和分布
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(100000, 999999);  // 生成6位数

        return dis(gen);
    }

    std::string loadfile(std::string filepath) {
        std::ifstream fin(filepath, std::ios::binary);

        if (!fin.is_open()) {
            std::cerr << "Error opening file: " << filepath << std::endl;
            return "";
        }

        // 或者使用 std::string 直接读取文件内容
        std::string content((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
        return content;
    }

    bool isMail(std::string text) {
        if (!rc["mail"].regex_id()) {
            rc["mail"] = rc.compile(R"(^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$)");
        }
        return regex_match(text, rc["mail"]);
    }

    bool sendVerificationCode(std::string to, std::string code) {
        const std::string smtp_password = "mxwzlusjmjlkbffc";
        const std::string username      = "shijiahansi@qq.com";

        if (isMail(to)) {
            try {
                smtp::Mail email;
                smtp::SmtpSSL smt;
                smt.login(username, smtp_password);

                email.set_form(username);
                email.set_to(to);
                email.set_subject("注册账号");
                // email.add_attachfile(R"(D:\Users\SGA\Pictures\63839470691664.png)");

                auto templatecode = loadfile("./smtp/verification_code.html");
                for (size_t i = 0; i < code.length(); i++) {
                    boost::algorithm::replace_all(templatecode, "${" + std::to_string(i) + "}", std::string(1, code[i]));
                }

                email.set_content(templatecode, "text/html");

                smt.mail(&email);
                smt.run();
                smt.quit();
                return true;
            }
            catch (const std::exception& e) {
                std::cerr << e.what() << '\n';
            }
        }
        return false;
    }
    std::vector<std::string_view> split(std::string_view content, std::string_view _split) {
        std::vector<std::string_view> list;
        std::string_view::size_type pos_start = 0;
        while (1) {
            auto pos = content.find(_split, pos_start);
            if (pos == std::string_view::npos) {
                if (pos_start < content.size()) {
                    list.push_back(content.substr(pos_start));
                }
                break;
            }
            if (pos == 0) {
                pos_start += _split.size();
                continue;
            }

            list.push_back(content.substr(pos_start, pos));
            pos_start = pos + _split.size();
            if (pos_start > content.size()) break;
        }
        return list;
    }

}  // namespace tools
